package com.lyf.boot.callback;

import com.lyf.boot.properties.MqttProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @author Luyufan
 * @date 2023/7/10 16:19
 */
@AllArgsConstructor
@Slf4j
public class BootMqttCallBack implements MqttCallback {

    private MqttProperties mqttProperties;

    /**
     * 客户端断开连接的回调
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
        log.info("{}与服务器断开连接", mqttProperties.getClientId());
    }

    /**
     * 消息到达的回调
     * @param topic
     * @param message
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) {
        log.info("接收消息主题 : {}", topic);
        log.info("接收消息Qos : {}", message.getQos());
        log.info("接收消息内容 : {}", new String(message.getPayload()));
        log.info("接收消息retained : {}", message.isRetained());
    }

    /**
     * 消息发布成功的回调
     * @param iMqttDeliveryToken
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        IMqttAsyncClient client = iMqttDeliveryToken.getClient();
        log.info("{}发布消息成功！", client.getClientId());
    }
}
